package javaweb_work01.main.Dao;

import javaweb_work01.login.Utils.DruidUtils;
import javaweb_work01.main.Entity.Students;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 访问数据库实现得到学员信息总数
 */
public class CountStudentsDaoImp implements CountStudentsDao{
    @Override
    public int countStudents() {
        try {
            //使用连接池创建QueryRunner对象
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            //编写sql语句
            String sql = "select count(num) from students";
            //执行语句
            Long query = qr.query(sql, new ScalarHandler<>());
            Integer i = Integer.valueOf(query.toString());
            //返回结果
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //如没有则返回0
        return 0;
    }
}
